<template>
	<el-date-picker v-model="viewModel" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions2"></el-date-picker>
</template>


<script>
	import api from 'api'
	import ala from 'ala'
	export default {
		model: {
			prop: 'dataModel',
			event: 'change'
		},
		props: {
			type: {},
			size: {},
			multiple: {
				default: false
			},
			apiUrl: {}, // Api网址，优先从Api中获取数据
			dataModel: {}
		},
		data() {
			return {
				value: '',
				pickerOptions2: {
					shortcuts: [{
							text: '最近1天',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								end.setTime(start.getTime() + 3600 * 1000 * 24 * 1)
								picker.$emit('pick', [start, end])
							}
						}, {
							text: '最近2天',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								end.setTime(start.getTime() + 3600 * 1000 * 24 * 2)
								picker.$emit('pick', [start, end])
							}
						},
						{
							text: '最近3天',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								end.setTime(start.getTime() + 3600 * 1000 * 24 * 3)
								picker.$emit('pick', [start, end])
							}
						},
						{
							text: '最近1周',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								end.setTime(start.getTime() + 3600 * 1000 * 24 * 7)
								picker.$emit('pick', [start, end])
							}
						}, {
							text: '最近15天',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								end.setTime(start.getTime() + 3600 * 1000 * 24 * 15)
								picker.$emit('pick', [start, end])
							}
						}, {
							text: '最近1个月',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								end.setTime(start.getTime() + 3600 * 1000 * 24 * 30)
								picker.$emit('pick', [start, end])
							}
						}, {
							text: '最近3个月',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								end.setTime(start.getTime() + 3600 * 1000 * 24 * 90)
								picker.$emit('pick', [start, end])
							}
						}
					]
				},
				viewModel: 0
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			async init() {
				this.viewModel = this.dataModel
				if (!this.viewModel) {
					const end = new Date()
					const start = new Date()
					end.setTime(start.getTime() + 3600 * 1000 * 24 * 7)
					this.viewModel = [start, end]
				}
			}
		},
		watch: {
			viewModel: {
				deep: true,
				handler(val) {
					this.$emit('change', this.viewModel)
				}
			}
		}
	}
</script>